package cn.hinglo.common.generator;

import com.jfinal.kit.StrKit;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @createTime: 2019/3/26 0026 9:09
 * @author: HingLo.C
 * @description: 用于Controller与Service的自动生成
 */
public class ControllerAndServiceGenerator {
    /***
     * 项目绝对路径,用于创建模板文件写入
     */
    private final static String basePath = "D:/Project/Maven/PermissionAdmin/src/main/java/";
    /***
     * 定义controller所在的包
     */
    private final static String controllerPackage = "cn.hinglo.permission.controller";
    /***
     * service所在包
     */
    private final static String servicePackage = "cn.hinglo.permission.service";

    /**
     * controller的模板文件
     */
    private final static String controllerTemplate = "cn/hinglo/common/generator/controllerTemplate.jf";
    /***
     * 服务层的模板文件
     */
    private final static String serviceTemplate = "cn/hinglo/common/generator/serviceTemplate.jf";


    public static void main(String[] args) throws IOException {

        // 类名称
        String className = "Api";
        Engine engine = Engine.create("myEngine");
        engine.setDevMode(true);
        engine.setToClassPathSourceFactory();
        engine.addSharedMethod(new StrKit());
        // 第一步创建controller
        createController(engine, className);
        // 第二步创建Service
        createService(engine, className);


    }

    /***
     * 创建controller
     * @param engine engine
     * @param className 实体名称
     * @throws IOException
     */
    private static void createController(Engine engine, String className) throws IOException {
        Map<String, Object> record = getInfo(1, className);
        Template template = engine.getTemplate(controllerTemplate);
        String dataController = template.renderToString(record);
        writeToFile(record, dataController);

    }

    /***
     * 创建服务层方法
     * @param engine engine
     * @param className 实体名称
     * @throws IOException
     */
    private static void createService(Engine engine, String className) throws IOException {
        // 第二步创建Service
        Map<String, Object> record = getInfo(2, className);
        Template template = engine.getTemplate(serviceTemplate);
        String dataService = template.renderToString(record);
        writeToFile(record, dataService);
    }

    /***
     * 通过type 用来生成service与controller
     * @param type 1:controller 2 service
     * @param className 类名称
     * @return 返回封装好的Map信息
     */
    private static Map<String, Object> getInfo(int type, String className) {
        Map<String, Object> record = new HashMap<>(4);
        record.put("packageName", type == 1 ? controllerPackage : servicePackage);
        record.put("servicePackage", servicePackage);
        record.put("className", className);
        String filePath = basePath + (type == 1 ? controllerPackage : servicePackage);
        filePath = filePath.replace(".", "/");
        String fileName = "/" + className + (type == 1 ? "Controller.java" : "Service.java");
        record.put("fileName", fileName);
        record.put("filePath", filePath);
        return record;
    }

    /**
     * 内容写入到文件总
     */
    private static void writeToFile(Map<String, Object> record, String data) throws IOException {
        String fileName = record.get("fileName").toString();
        String filePath = record.get("filePath").toString();
        File dir = new File(filePath);
        if (!dir.exists()) {
            boolean res = dir.mkdirs();
            if (!res) {
                throw new RuntimeException("文件夹创建失败,无法创建模板文件");
            }
        }
        try (OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filePath + fileName), StandardCharsets.UTF_8)) {
            osw.write(data);
        }
    }
}
